package com.codeoss.framework.view.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.util.DependObject;

public class Page extends Container {
	private String pageBean;
	private String code;
	private String name;
	private List<DataSourceConfig> dataSourceConfigList;
	private DependObject dependObject=new DependObject();
	private Map<String,DataSourceConfig> dataSourceConfigMap=new HashMap<String,DataSourceConfig>();
	
	private boolean init;
	
	private Map<String,String> pageContextMap=new HashMap<String,String>();
	
	public String getPageBean() {
		return pageBean;
	}

	public void setPageBean(String pageBean) {
		this.pageBean = pageBean;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	public List<DataSourceConfig> getDataSourceConfigList() {
		return dataSourceConfigList;
	}

	public void setDataSourceConfigList(List<DataSourceConfig> dataSourceConfigList) {
		this.dataSourceConfigList = dataSourceConfigList;
		dataSourceConfigMap.clear();
		if(dataSourceConfigList!=null) {
			for (int i = 0; i < dataSourceConfigList.size(); i++) {
				dataSourceConfigMap.put(dataSourceConfigList.get(i).getId(), dataSourceConfigList.get(i));
			}
		}
	}

	public DataSourceConfig getDataSourceConfigById(String id) {
		return dataSourceConfigMap.get(id);
	}
	
	public DependObject getDependObject() {
		return dependObject;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Component findComponentByVarName(String varName) {
		List<IDataBinding> dataBindingList=new ArrayList<IDataBinding>();
		innerGetFormFieldList(this,dataBindingList);
		for(int i=0;i<dataBindingList.size();i++) {
			if(varName.equals(((IVarName)dataBindingList.get(i)).getVarName())){
				return (Component)dataBindingList.get(i);
			}
		}
		return null;
	}
	
	protected void innerGetFormFieldList(Component component, List<IDataBinding> dataBindingList) {
		if (component instanceof Container) {
				Container c = (Container) component;
				for (int i = 0; i < c.getChildren().size(); i++) {
					innerGetFormFieldList(c.getChildren().get(i), dataBindingList);
				}
		} else {
			if (component instanceof IDataBinding) {
				if (!component.getType().equals("tree")) {
					IDataBinding dataBinding = (IDataBinding) component;
					if (!StringUtils.isEmpty(dataBinding.getDataField())) {
						dataBindingList.add((IDataBinding) component);
					}
				}
			}
		}
	}
	
	public boolean isInit() {
		return init;
	}

	public void setInit(boolean init) {
		this.init = init;
	}

	public void addPageContext(String key,String value) {
		pageContextMap.put(key, value);
	}
	
	public String getPageContext(String key){
		return pageContextMap.get(key);
	}
	
	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return "page";
	}

}
